package com.example.huangds.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends Activity {
    private List<GoodsInfo> goodsInfoList = new ArrayList<GoodsInfo>();
    private  ListView listview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 加载商品列表数据
        initGoods();

        GoodsListAdapter adapter = new GoodsListAdapter(MainActivity.this, R.layout.goods_list, goodsInfoList, this);
        listview = (ListView) findViewById(R.id.list_view);
        listview.setAdapter(adapter);

    }

    // 加载商品列表数据
    private void initGoods() {
        try {
            Map<String, String> paramMap = new HashMap<String, String>();
            paramMap.put("c", "kyunshop002");
            paramMap.put("start", "0");
            paramMap.put("pageSize", "20");

            List<Map<String, Object>> resultJsonList = HttpUtil.excutePostJsonList(Constants.INTERFACE_BASE_URL, paramMap);
            for (Map<String, Object> jsonMap : resultJsonList) {
                GoodsInfo goodsInfo = new GoodsInfo();
                String imagePath = (String) jsonMap.get("picture");
                goodsInfo.setImageUrl(Constants.PICTURES_URL + imagePath);

                String goodsNm = (String) jsonMap.get("name");
                goodsInfo.setGoodsNm(goodsNm);
                String goodsDesc = (String) jsonMap.get("introduce");
                goodsInfo.setGoodsDesc(goodsDesc);
                BigDecimal nowPrice = (BigDecimal) jsonMap.get("nowPrice");
                goodsInfo.setNewPrice("¥" + nowPrice);
                Integer sellcount = (Integer) jsonMap.get("sellcount");
                goodsInfo.setGoodsSell("销量 " + sellcount);
                Integer stock = (Integer) jsonMap.get("stock");
                goodsInfo.setGoodsStore("库存 " + stock);
                goodsInfoList.add(goodsInfo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
